package org.yan.stock.server.client;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

/**
 * Client User
 * @author yanxuxin_sz
 */
@PersistenceCapable
public class Client implements Serializable {

	private static final long serialVersionUID = 5390738467777887591L;
	
	/**
	 * Device-id can be specified by device, default is hash of abs(registration
     * id).
     * user@example.com
	 */
	@PrimaryKey
    @Persistent
    private Key key;
	
	/**
	 * The ID used for sending messages to.
	 */
	@Persistent
	private String authId; 
	
	/**
	 * The email of registration client
	 */
	@Persistent
	private String senderId;
	
	@Persistent
	private String stocks;
	
	@Persistent
	private String latchs;
	
	/**
	 * Android, iOS and so on.
	 */
	@Persistent
	private String os;
	
	@Persistent
	private Date regDate;
	
	@Persistent
	private Date upDate;
	
	/**
	 * The type of client, 
	 * 0:free,1:paid
	 */
	@Persistent
	private String paidType;
	
	private Stock[] stockArr;
	
	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public String getAuthId() {
		return authId;
	}

	public void setAuthId(String authId) {
		this.authId = authId;
	}

	public String getSenderId() {
		return senderId;
	}

	public void setSenderId(String senderId) {
		this.senderId = senderId;
	}

	public String getStocks() {
		return stocks;
	}

	public void setStocks(String stocks) {
		this.stocks = stocks;
	}

	public String getLatchs() {
		return latchs;
	}

	public void setLatchs(String latchs) {
		this.latchs = latchs;
	}

	public String getOs() {
		return os;
	}

	public void setOs(String os) {
		this.os = os;
	}

	public Date getRegDate() {
		return regDate;
	}

	public void setRegDate(Date regDate) {
		this.regDate = regDate;
	}

	public Date getUpDate() {
		return upDate;
	}

	public void setUpDate(Date upDate) {
		this.upDate = upDate;
	}

	public String getPaidType() {
		return paidType;
	}

	public void setPaidType(String paidType) {
		this.paidType = paidType;
	}

	public Stock[] getStockArr() {
		if(stockArr == null) {
			String[] stCodes = getStocks().split(",");
			String[] stLatchs = getLatchs().split(",");
			if(stCodes.length != stLatchs.length) {
				return null;
			}
			this.stockArr = new Stock[stCodes.length];
			Stock tmp = null;
			for(int i = 0; i < stCodes.length; i++) {
				tmp = new Stock();
				tmp.setCode(stCodes[i]);
				tmp.setLatch(Double.valueOf(stLatchs[i]));
				stockArr[i] = tmp;
			}
		}
		return stockArr;
	}

	public void setStockArr(Stock[] stockArr) {
		this.stockArr = stockArr;
	}

}
